﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace DirectedGraphCycle
{
    public partial class MainForm : Form
    {

        public MainForm()
        {
            InitializeComponent();
        }

        private void chooseFile_button_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Title = "Open File";
            DialogResult result = openDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.fileNam_textBox.Text = openDialog.FileName;
            }
        }

        private void btnChooseOutputDir_Click(object sender, EventArgs e)
        {
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                txtOutputDir.Text = fbd.SelectedPath;
            }
        }

        public void printNumOfCycleToGUI(int numOfCycle)
        {
            allCycles_richTextBox.Text = numOfCycle.ToString("#,##0") + Environment.NewLine + allCycles_richTextBox.Text;
        }

        public void printFirstCyclesToGUI(string line)
        {
            allCycles_richTextBox.Text = line;
        }

        public void printAppendCyclesToGUI(string line)
        {
            allCycles_richTextBox.Text += line;
        }

        private void running_button_Click(object sender, EventArgs e)
        {
            allCycles_richTextBox.Text = "";
            runningTime_textBox.Text = "";
            FindAllCycle findAllCyle = null;
            int start = Environment.TickCount;
            if (directed_radioButton.Checked)
            {
                findAllCyle = new FindAllDirectedCycle();
            }
            else
            {
                findAllCyle = new FindAllDirectedCycle();
            }
            findAllCyle.filePathInput = this.fileNam_textBox.Text;
            findAllCyle.readGraphData();
            findAllCyle.GUI = this;
            if (chkOutToFile.Checked)
            {
                findAllCyle.filePathOutput = Path.Combine(txtOutputDir.Text, "GRAPH.OUT");
                //findAllCyle.printFirstCyclesToFile();
            }
            else
            {
                string filePath = Path.Combine(Path.GetDirectoryName(this.fileNam_textBox.Text), "GRAPH.OUT");
                findAllCyle.filePathOutput = filePath;
            }

            findAllCyle.getAllCycles();
            findAllCyle.printFinalCyclesToFile();
            int duration = Environment.TickCount - start;
            this.runningTime_textBox.Text = duration.ToString("#,##0");
        }

    }
}
